﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;

namespace UdrinkWater_Project.Admin
{
    public partial class ReportShopFront : System.Web.UI.Page
    {
        private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            string OrderID = Session["Date"].ToString() + " " + Session["Time"].ToString();
            string list = Session["list"].ToString();
            string SumAll = Session["SumAll"].ToString();
            string PROVINCE_NAME = Session["PROVINCE_NAME"].ToString();
            string DISTRICT_NAME = Session["DISTRICT_NAME"].ToString();
            string AMPHUR_NAME = Session["AMPHUR_NAME"].ToString();
            string Zipcode = Session["Zipcode"].ToString();
           


            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();

            DataSet ds = new DataSet();
            DataTable dt = null;
            string strConnString = null;
            StringBuilder strSQL = new StringBuilder();
            if (Session["GuestEmp"].ToString() == "false")
            {
                strSQL.Append("  SELECT         ROW_NUMBER() OVER(ORDER BY TB_Order.Order_Date DESC) AS Row,TB_Order.Order_Date, TB_Order.Customer_ID, (TB_Customer.Prefix +' '+ TB_Customer.Firstname +' '+ TB_Customer.Lastname) AS CusName, TB_Order.Shipped_date, TB_Order.Delivery_Address,  ");
                strSQL.Append(" TB_Order.Employee_ID,( TB_Employee.Prefix +' '+ TB_Employee.Firstname +' '+ TB_Employee.Lastname) AS EmpName, TB_Orderlist.Product_ID, TB_Product.Product_name,");
                strSQL.Append(" TB_Unit_Category.Unit_Name, TB_Orderlist.Product_Count, TB_Orderlist.Price, TB_Orderlist.Discount,((TB_Orderlist.Product_Count * TB_Orderlist.Price) - TB_Orderlist.Discount) AS Total ");
                strSQL.Append(" FROM            TB_Customer LEFT JOIN ");
                strSQL.Append(" TB_Order LEFT JOIN ");
                strSQL.Append(" TB_Orderlist ON TB_Order.Order_Date = TB_Orderlist.Order_Date LEFT JOIN ");
                strSQL.Append(" TB_Employee ON TB_Order.Employee_ID = TB_Employee.ID_Card LEFT JOIN ");
                strSQL.Append(" TB_Product LEFT JOIN ");
                strSQL.Append(" TB_Unit_Category ON TB_Product.Unit = TB_Unit_Category.Unit_ID ON TB_Orderlist.Product_ID = TB_Product.Product_ID ON TB_Customer.ID_Card = TB_Order.Customer_ID ");
                strSQL.Append(" WHERE TB_Order.Order_Date = '" + OrderID + "'");
            }
            if (Session["GuestEmp"].ToString() == "true")
            {
                strSQL.Append("  SELECT         ROW_NUMBER() OVER(ORDER BY TB_Order.Order_Date DESC) AS Row,TB_Order.Order_Date, ('-') AS Customer_ID, ('"+Session["CusName"].ToString()+"') AS CusName, TB_Order.Shipped_date, TB_Order.Delivery_Address,  ");
                strSQL.Append(" TB_Order.Employee_ID,( TB_Employee.Prefix +' '+ TB_Employee.Firstname +' '+ TB_Employee.Lastname) AS EmpName, TB_Orderlist.Product_ID, TB_Product.Product_name,");
                strSQL.Append(" TB_Unit_Category.Unit_Name, TB_Orderlist.Product_Count, TB_Orderlist.Price, TB_Orderlist.Discount,((TB_Orderlist.Product_Count * TB_Orderlist.Price) - TB_Orderlist.Discount) AS Total ");
                strSQL.Append(" FROM ");
                strSQL.Append(" TB_Order LEFT JOIN ");
                strSQL.Append(" TB_Orderlist ON TB_Order.Order_Date = TB_Orderlist.Order_Date LEFT JOIN ");
                strSQL.Append(" TB_Employee ON TB_Order.Employee_ID = TB_Employee.ID_Card LEFT JOIN ");
                strSQL.Append(" TB_Product LEFT JOIN ");
                strSQL.Append(" TB_Unit_Category ON TB_Product.Unit = TB_Unit_Category.Unit_ID ON TB_Orderlist.Product_ID = TB_Product.Product_ID ");
                strSQL.Append(" WHERE TB_Order.Order_Date = '" + OrderID + "'");
            }

            
               
            
            


            objConn.ConnectionString = Connection;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL.ToString();
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;

            dtAdapter.Fill(ds, "ShopReportOrder");
            dt = ds.Tables[0];

            dtAdapter = null;
            objConn.Close();
            objConn = null;

            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("~/Officer/ShopReportOrder.rpt"));

            rpt.SetDataSource(dt);


            rpt.SetParameterValue("list", list);
            rpt.SetParameterValue("SumAll", SumAll);
            rpt.SetParameterValue("PROVINCE_NAME", PROVINCE_NAME);
            rpt.SetParameterValue("AMPHUR_NAME", AMPHUR_NAME);
            rpt.SetParameterValue("DISTRICT_NAME", DISTRICT_NAME);
            rpt.SetParameterValue("Zipcode", Zipcode);

            //this.ShopOrderReport.ReportSource = rpt;
            rpt.ExportToHttpResponse
            (CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "Receipt");
        }
    }
}